package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.stereotype.Controller;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSerialNumber;
import com.ruoyi.system.service.ISysSerialNumberService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 序列号模板Controller
 * 
 * @author SunLuQuan
 * @date 2022-06-24
 */
@Controller
@RequestMapping("/system/serialNumber")
public class SysSerialNumberController extends BaseController
{
    private String prefix = "system/serialNumber";

    @Autowired
    private ISysSerialNumberService sysSerialNumberService;

    @RequiresPermissions("system:serialNumber:view")
    @GetMapping()
    public String serialNumber()
    {
        return prefix + "/serialNumber";
    }

    @GetMapping("/solutionSelection")
    public String serialNumberSolutionSelection()
    {
        return prefix + "/serialNumberSolutionSelection";
    }

    /**
     * 获取最大的序列号
     * @param moduleCode
     * @return
     */
    @GetMapping("/getMaxSerialNumber/{moduleCode}")
    @ResponseBody
    public AjaxResult getMaxSerialNumber(@PathVariable("moduleCode") String moduleCode){

        String maxSerialNumber = sysSerialNumberService.getMaxSerialNumberByCode(moduleCode);

        return AjaxResult.success("操作成功！",maxSerialNumber);
    }

    /**
     * 查询序列号模板列表
     */
    @RequiresPermissions("system:serialNumber:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSerialNumber sysSerialNumber)
    {
        startPage();
        List<SysSerialNumber> list = sysSerialNumberService.selectList(sysSerialNumber);
        return getDataTable(list);
    }

    /**
     * 查询简单的序列号模板列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(SysSerialNumber sysSerialNumber){

        Collection<SysSerialNumber> sysSerialNumberList = sysSerialNumberService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(sysSerialNumberList,SysSerialNumber::getModuleCode,SysSerialNumber::getModuleName));
    }

    /**
     * 获取序列号模板详细信息
     */
    @RequiresPermissions("system:serialNumber:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysSerialNumberService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param sysSerialNumber 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(SysSerialNumber sysSerialNumber){
        int weigth = sysSerialNumberService.getMaxOfWeight(sysSerialNumber);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载序列号模板导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysSerialNumber> util = new ExcelUtil<>(SysSerialNumber.class);
        util.importTemplateExcel(response, "序列号模板数据");
    }

    /**
     * 导入序列号模板数据
     * @param file 序列号模板数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "序列号模板", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:serialNumber:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysSerialNumber> util = new ExcelUtil<>(SysSerialNumber.class);
        List<SysSerialNumber> sysSerialNumberList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(sysSerialNumberService.updateBatchById(sysSerialNumberList)){
                return AjaxResult.success("导入序列号模板数据覆盖成功！");
            }
            return AjaxResult.error("导入序列号模板数据覆盖失败！");
        }
        if(sysSerialNumberService.insertBatch(sysSerialNumberList)){
            return AjaxResult.success("导入序列号模板数据新增成功！");
        }
        return AjaxResult.error("导入序列号模板数据新增失败！");
    }

    /**
     * 导出序列号模板列表
     */
    @RequiresPermissions("system:serialNumber:export")
    @Log(title = "序列号模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSerialNumber sysSerialNumber)
    {
        List<SysSerialNumber> list = sysSerialNumberService.selectList(sysSerialNumber);
        ExcelUtil<SysSerialNumber> util = new ExcelUtil<SysSerialNumber>(SysSerialNumber.class);
        return util.exportExcel(list, "序列号模板数据");
    }

    /**
     * 新增序列号模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存序列号模板
     */
    @RequiresPermissions("system:serialNumber:add")
    @Log(title = "序列号模板", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody SysSerialNumber sysSerialNumber)
    {
        return toAjax(sysSerialNumberService.insert(sysSerialNumber));
    }

    /**
     * 修改序列号模板
     */
    @RequiresPermissions("system:serialNumber:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysSerialNumber sysSerialNumber = sysSerialNumberService.selectById(id);
        mmap.put("sysSerialNumber", sysSerialNumber);
        return prefix + "/edit";
    }

    /**
     * 修改保存序列号模板
     */
    @RequiresPermissions("system:serialNumber:edit")
    @Log(title = "序列号模板", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody SysSerialNumber sysSerialNumber)
    {
        return toAjax(sysSerialNumberService.updateById(sysSerialNumber));
    }

    /**
     * 修改序列号模板 状态 根据 ids 主键
     */
    @RequiresPermissions("system:serialNumber:edit")
    @Log(title = "序列号模板", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Long> stateVo)
    {
        return toAjax(sysSerialNumberService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 删除序列号模板
     */
    @RequiresPermissions("system:serialNumber:remove")
    @Log(title = "序列号模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Long> ids)
    {
            return toAjax(sysSerialNumberService.deleteByIds(ids));
    }
}
